<?php

require_once LIBRARIES_ENTITIES_CONFIGURATION;
require_once LIBRARIES_TOOLS_UTILITY;
require_once LIBRARIES_TOOLS_XTEMPLATE;

class ConfigurationController {

    public function invoke() {
        // function list
        $groupFunctions = $_SESSION['groupFunctions'];

        // directory path
        $dir = dirname(__FILE__);

        $action = '';
        if (isset($_POST['hdAction'])) {
            $action = $_POST['hdAction'];
        }

        if ($action == '') {
            if (Utility::checkPermission('view', $groupFunctions, CONFIGURATION_MANAGEMENT)) {
                $viewFile = $dir . '/views/configuration_edit.html';
                $xtemplate = new XTemplate($viewFile);

                $configuration = new Configuration();
                $xmlFile = ROOT . DS . 'configuration.xml';
                $configuration->getConfiguration($xmlFile);

                $siteName = $configuration->getSiteName();
                $marquee = $configuration->getMarquee();
                $offlineMessage = $configuration->getOfflineMessage();
                $offline = $configuration->getOffline();
                $email = $configuration->getEmail();
                $address = $configuration->getAddress();
                $phoneNumber = $configuration->getPhoneNumber();

                $xtemplate->assign('SITE_NAME', $siteName);
                $xtemplate->assign('MARQUEE', $marquee);
                $xtemplate->assign('OFFLINE_MESSAGE', $offlineMessage);
                $xtemplate->assign('EMAIL', $email);
                $xtemplate->assign('ADDRESS', $address);
                $xtemplate->assign('PHONE_NUMBER', $phoneNumber);

                if ($offline == 1) {
                    $xtemplate->parse('main.status_false');
                } else {
                    $xtemplate->parse('main.status_true');
                }

                // check edit permission
                if (Utility::checkPermission('update', $groupFunctions, CONFIGURATION_MANAGEMENT)) {
                    $xtemplate->parse('main.update_button');
                }
                $xtemplate->parse('main');
                $xtemplate->out('main');
            }
        } else if ($action == 'save') {
            $siteName = $_POST['txtSiteName'];
            $marquee = $_POST['txtMarquee'];
            $offlineMessage = $_POST['txtOfflineMessage'];
            $offline = $_POST['offline'];
            $email = $_POST['txtEmail'];
            $address = $_POST['txtAddress'];
            $phoneNumber = $_POST['txtPhoneNumber'];

            $configuration = new Configuration();
            $configuration->setSiteName($siteName);
            $configuration->setMarquee($marquee);
            $configuration->setOfflineMessage($offlineMessage);
            $configuration->setOffline($offline);
            $configuration->setEmail($email);
            $configuration->setPhoneNumber($phoneNumber);
            $configuration->setAddress($address);

            $xmlFile = ROOT . DS . 'configuration.xml';
            $result = $configuration->updateConfiguration($xmlFile);
            echo $result;
            Utility::messageBox('Cập nhật thành công', './?com=com_configuration');
        }
    }

}

?>
